set more off
set scheme s1color
set graph off


* cd // specify path


********************************************************************************
****************************** Graphs & Tables *********************************
********************************************************************************


********************************************************************************
************************************ Section 3 *********************************

********************************************************************************
use tri_nlrb_pollution_sample, clear

*** Figure 1: Number of Union Elections and Vote Shares by Election Year
preserve
keep if year>electionyear // Post Election Effects
keep if city_samp==1 //& voters>=50
duplicates drop case_number trifd, force // Elections on the Facility Level
sort electionyear
by electionyear, sort: egen yX=mean(vshare)
by electionyear, sort: egen yseX=semean(vshare)

gen upyX = yX+yseX
gen lowyX = yX-yseX

label variable yX "Vote share for union"
label variable upyX "CI vote share"
label variable lowyX "CI vote share"

by electionyear, sort: gen yn=_N
// encode case_number, gen(ncase_number)
// by electionyear, sort: egen yn=count(ncase_number)

label variable yn "Elections"

duplicates drop electionyear, force

# delimit ;
twoway rcap upyX lowyX electionyear, lstyle(ci) lcolor(gs10)  
	|| scatter yX electionyear, xtitle("Election year",size(medsmall))
	xlabel(1990(3)2017) xmtick(##3)
	ytitle("Vote share for union [%]", axis(1) size(medsmall)) 
	ylabel(30(10)70, axis(1) angle(horizontal)) ymtick(##2, axis(1))
	xscale(titlegap(1) outergap(2))
	connect(l) lwidth(thin) msymbol(O) msize(medsmall) lcolor(gs3) mcolor(gs5) 
	|| line yn electionyear, yaxis(2) lpattern(dash)
	ytitle("Elections [#]", size(medsmall) axis(2))
	ylabel(20(20)100,axis(2) angle(horizontal)) ymtick(##2, axis(2))
	legend(rows(1) size(medsmall) symx(*.75) colgap(.9));
#delimit cr
graph export "results\ElectionsVshareAll.pdf", replace
restore

********************************************************************************
*** Table 1: Pre-Treatment Balance
*** Difference in Observable Characteristics Between Unionized and Nonunionized Firms
* Paydex score, sales and employees are part of NETS proprietary data and are not included in the replication data
use tri_nlrb_pollution_sample, clear

local f (fmt(3))
eststo clear
#delimit ;
estpost ttest ratrelease_sum rattotal_onsite_releases rattotal_offsite_releases ratmanaged_sum rattotal_onsite_waste_man ratoffsite_waste_man prod_ratio_mean catastrophic_releases_dum pp_inno    
if voters>=50 & city_samp==1 & nvshare>=41 & nvshare<=59 & year==electionyear-1, by(T);
esttab . using "results\tables.tex",
	cells("mu_1`f' mu_2`f' b`f' t`f' p`f'")	nonumbers modelw(10) title("T1: Pre-Treatment Balance") label noobs replace;
#delimit cr
eststo clear
** AQI
preserve
duplicates drop uniqueid year, force //Pollution data on the facility-year level
local f (fmt(3))
eststo clear
estpost ttest aqione rataqione if voters>=50 & city_samp==1 & nvshare>=41 & nvshare<=59 & year==electionyear-1, by(T)
esttab . using "results\tables.tex", 	cells("mu_1`f' mu_2`f' b`f' t`f' p`f'")	nonumbers modelw(10) title("T1: Pre-Treatment Balance") label noobs append
restore

**** Manipulation Tests
********************************************************************************
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
preserve
duplicates drop case_number trifd, force // Elections on the Facility Level
sort case_number

*** Figure 2: Distribution of Vote Shares
# delimit ;
hist nvshare if voters>=50 & city_samp==1 , width(2) start(0) xline(50, 
	lcolor(edkblue)) xtitle("Vote share for union") 
	ytitle("Density") legend(off) fcolor(edkblue*0.6) lcolor(edkblue); //kdensity
# delimit cr
graph export "results\HistSample.pdf", as(pdf) replace

*** Appendix Figure 2: Cattaneo (2020) Discontinuity Test
rddensity nvshare  if voters>=50 & city_samp==1, c(50) p(1) bwselect(each) plot graph_opt(xtitle("Vote share") ytitle("Density estimates") legend(off) scheme(s1color))
graph export "results\CattaneoEachSample.pdf", as(pdf) replace

*** Appendix Figure 3: McCrary (2008) Discontinuity Test
DCdensity nvshare if voters>=50 & city_samp==1, breakpoint(50) generate(Xj Yj r0 fhat se_fhat) 
drop Xj Yj r0 fhat se_fhat
graph export "results\MccrarySample.pdf", as(pdf) replace
restore

********************************************************************************
************************************ Section 4 *********************************
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

*** Table 2 & 3: Main Analysis - Waste Releases and Treatment after Unionization
global ratios ratrelease_sum rattotal_onsite_releases rattotal_offsite_releases 
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	sum `v' if voters>=50 & city_samp==1 & nvshare >=50-e(h_l) 
	estadd scalar mean=r(mean)
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
title("T2: Main Analysis") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** 
global ratios ratmanaged_sum rattotal_onsite_waste_man ratoffsite_waste_man
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	sum `v' if voters>=50 & city_samp==1 & nvshare >=50-e(h_l) 
	estadd scalar mean=r(mean)
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
title("T3: Main Analysis") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append


*** Table 4: Real Effect Analysis - Air Quality Index
preserve
duplicates drop uniqueid year, force //Pollution data on the facility-year level

eststo clear
eststo, title("AQI"): rdrobust aqione nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
sum aqione if voters>=50 & city_samp==1 & nvshare >=50-e(h_l) & nvshare <50
estadd scalar mean=r(mean)
	
eststo, title("Change"): rdrobust rataqione nvshare if voters>=50 & city_samp==1 , c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
sum rataqione if voters>=50 & city_samp==1 & nvshare >= 50-e(h_l) & nvshare < 50
estadd scalar mean=r(mean) //rataqimean works with whole sample

esttab using "results\tables.tex", drop(Conventional Bias-corrected) title("T4: Air Quality Index") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
restore

* untabulated (within one mile)
preserve
duplicates drop uniqueid year, force //Pollution data on the facility-year level
keep if d_miles<=1

eststo clear
eststo, title("AQI"): rdrobust aqione nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
sum aqione if voters>=50 & city_samp==1 & nvshare >=50-e(h_l) & nvshare <50
estadd scalar mean=r(mean)
	
eststo, title("Change"): rdrobust rataqione nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
sum rataqione if voters>=50 & city_samp==1 & nvshare >= 50-e(h_l) & nvshare < 50
estadd scalar mean=r(mean) //rataqimean works with whole sample

esttab using "results\tables.tex", drop(Conventional Bias-corrected) title("Untabulated (within 1 mile)") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
restore


********************************************************************************
*** Appendix Table 3: Waste Release and Treatment by Year
use tri_nlrb_pollution_sample.dta, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

global ratios ratrelease_sum rattotal_onsite_releases rattotal_offsite_releases ratmanaged_sum rattotal_onsite_waste_man ratoffsite_waste_man

*** Panel A changes in releases
eststo clear
eststo, title("t+1"): rdrobust ratrelease_sum nvshare if voters>=50 & city_samp==1 & year==electionyear+1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+2"):rdrobust ratrelease_sum nvshare if voters>=50 & city_samp==1 & year==electionyear+2, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+3"):rdrobust ratrelease_sum nvshare if voters>=50 & city_samp==1 & year==electionyear+3, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)

esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT3: Panel A: Waste releases by year") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) replace

*** Panel B changes in onsite releases
eststo clear
eststo, title("t+1"): rdrobust rattotal_onsite_releases nvshare if voters>=50 & city_samp==1 & year==electionyear+1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+2"):rdrobust rattotal_onsite_releases nvshare if voters>=50 & city_samp==1 & year==electionyear+2, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+3"):rdrobust rattotal_onsite_releases nvshare if voters>=50 & city_samp==1 & year==electionyear+3, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)

esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT3: Panel B: On-site waste releases by year") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
	
*** Panel C changes in treatment
eststo clear
eststo, title("t+1"): rdrobust ratmanaged_sum nvshare if voters>=50 & city_samp==1 & year==electionyear+1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+2"): rdrobust ratmanaged_sum nvshare if voters>=50 & city_samp==1 & year==electionyear+2, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+3"): rdrobust ratmanaged_sum nvshare if voters>=50 & city_samp==1 & year==electionyear+3, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT3: Panel C: Waste treatment by year") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Panel D changes in onsite treatment
eststo clear
eststo, title("t+1"): rdrobust rattotal_onsite_waste_man nvshare if voters>=50 & city_samp==1 & year==electionyear+1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+2"): rdrobust rattotal_onsite_waste_man nvshare if voters>=50 & city_samp==1 & year==electionyear+2, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
eststo, title("t+3"): rdrobust rattotal_onsite_waste_man nvshare if voters>=50 & city_samp==1 & year==electionyear+3, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT3: Panel D: On-site waste treatment by year") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
	

* Panel E Air Quality Index
preserve
duplicates drop uniqueid year, force //Pollution data on the facility-year level

eststo clear
eststo, title("t+1"): rdrobust aqione nvshare if voters>=50 & city_samp==1 & year==electionyear+1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
eststo, title("t+2"): rdrobust aqione nvshare if voters>=50 & city_samp==1 & year==electionyear+2, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
eststo, title("t+3"): rdrobust aqione nvshare if voters>=50 & city_samp==1 & year==electionyear+3, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT3: Panel E: AQI by year") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) restore

* Panel F AQI ratio	
eststo clear
eststo, title("t+1"): rdrobust rataqione nvshare if voters>=50 & city_samp==1 & year==electionyear+1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
eststo, title("t+2"): rdrobust rataqione nvshare if voters>=50 & city_samp==1 & year==electionyear+2, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
eststo, title("t+3"): rdrobust rataqione nvshare if voters>=50 & city_samp==1 & year==electionyear+3, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT3: Panel F: AQI ratio by year") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label mtitles star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
restore

********************************************************************************
*** Table 5: Longer-Term Effects on Waste Management and Air Pollution	
use tri_nlrb_pollution_sample.dta, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+5 //Up to five years post election

global ratios ratrelease_sum rattotal_onsite_releases ratmanaged_sum rattotal_onsite_waste_man 

*** First five years
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	sum `v' if voters>=50 & city_samp==1 & nvshare >=50-e(h_l)  &nvshare <50
	estadd scalar mean=r(mean)
}

preserve
duplicates drop uniqueid year, force
eststo, title("AQI"): rdrobust aqione nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
sum rataqione if voters>=50 & city_samp==1 & nvshare >= 50-e(h_l) & nvshare < 50
estadd scalar mean=r(mean)
restore

esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
title("T5: Longer Term Effects Panel A: First five years)") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

** Five years without first two years
preserve
drop if year==electionyear+1 
drop if year==electionyear+2

eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	sum `v' if voters>=50 & city_samp==1 & nvshare >=50-e(h_l)  &nvshare <50
	estadd scalar mean=r(mean)
}

duplicates drop uniqueid year, force
eststo, title("AQI"): rdrobust aqione nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
sum rataqione if voters>=50 & city_samp==1 & nvshare >= 50-e(h_l) & nvshare < 50
estadd scalar mean=r(mean)
restore

esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
title("T5: Longer Term Effects Panel B: Three-to-five years") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append


*** Appendix Table 4: Real Effect Analysis - Limited Sample Without environmental violations
use tri_nlrb_echo_sample, clear
keep if year>electionyear  // Post Election Effects
keep if year<=electionyear+3 // Up to three years post election

global ratios ratrelease_sum rattotal_onsite_releases ratmanaged_sum rattotal_onsite_waste_man


* check how far away violation and election dates -> nrcontviolations (those within our examination period)
gen nrcontviolations=.
replace nrcontviolations = nrviolations if (violationyear1<=electionyear+3 & violationyear1!=.) | (violationyear2<=electionyear+3 & violationyear2!=.) | (violationyear3<=electionyear+3 & violationyear3!=.)

** Main Spec: stronger and better results if leave out facilities that ever reported a violation
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & nrviolations==., c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	sum `v' if voters>=50 & city_samp==1 & nvshare >=50-e(h_l) 
	estadd scalar mean=r(mean)
}

esttab using "results/appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT4 Panel A: Facilities without reporting violation") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

** Main Spec: stronger and better results if leave out facilities that reported a violation within 3 years after electoins, but not a lot of change
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & nrcontviolations==., c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	sum `v' if voters>=50 & city_samp==1 & nvshare >=50-e(h_l) 
	estadd scalar mean=r(mean)
}

esttab using "results/appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT4 Panel B: Facilities without reporting violation 3 years after elections") stats(N mean, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append


*******************************************************************
***** ROBUSTNESS 
********************************************************************************
use tri_nlrb_pollution_sample.dta, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election_count

*** Appendix Figure 4: Global Polynomial Discontinuity Estimates
*** Released waste
#delimit ;
rdplot ratrelease_sum nvshare if voters>=50 & city_samp==1, c(50) vce(cluster id)
	p(3) graph_options(graphregion(color(white)) 
	xtitle(Vote share) ytitle(Change in released waste)) saving(releaseall,replace);
#delimit cr
graph export "results\rdplotRatreleasedGlo.pdf", as(pdf) replace

*** On-site released waste
#delimit ;
rdplot rattotal_onsite_releases nvshare if voters>=50 & city_samp==1, c(50) vce(cluster id)
	p(3) graph_options(graphregion(color(white)) 
	xtitle(Vote share) ytitle(Change in on-site released waste)) saving(onreleaseall,replace);
#delimit cr
graph export "results\rdplotRatonreleasedGlo.pdf", as(pdf) replace

*** Treated waste
#delimit ;
rdplot ratmanaged_sum nvshare if voters>=50 & city_samp==1, c(50) vce(cluster id)
	p(3) graph_options(graphregion(color(white)) 
	xtitle(Vote share) ytitle(Change in treated waste)) saving(cureall,replace);
#delimit cr
graph export "results\rdplotRatmanagedGlo.pdf", as(pdf) replace

* On-site treated waste 
#delimit ;
rdplot rattotal_onsite_waste_man nvshare if voters>=50 & city_samp==1, c(50) vce(cluster id)
	p(3) graph_options(graphregion(color(white)) 
	xtitle(Vote share) ytitle(Change in on-site treated waste)) saving(oncureall,replace);
#delimit cr
graph export "results\rdplotRatonmanagedGlo.pdf", as(pdf) replace

* AQI
preserve
duplicates drop uniqueid year, force
rdplot aqione nvshare if voters>=50 & city_samp==1, c(50) vce(cluster id) p(3) graph_options(graphregion(color(white)) xtitle(Vote share) ytitle(Air Quality Index)) saving(releaseall,replace)
graph export "results\rdplotAQIGlo.pdf", as(pdf) replace
* AQI ratio
rdplot rataqione nvshare if voters>=50 & city_samp==1, c(50) vce(cluster id) p(3) graph_options(graphregion(color(white)) xtitle(Vote share) ytitle(Change in Air Quality Index)) saving(releaseall,replace)
graph export "results\rdplotratAQIGlo.pdf", as(pdf) replace
restore

*** Appendix Table 5: Global Regression Discontinuity (Third-Order Polynomial) - Waste Management
*** only conventional estimates
gen X = nvshare-50
gen T_X = X * T
gen weights = .
replace weights = (1 - abs(X / 50 )) if X <= 0 & X >= -50 
replace weights = (1 - abs(X / 50 )) if X > 0 & X <= 50

eststo clear
eststo: reg ratrelease_sum i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id)
eststo: reg rattotal_onsite_releases i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id)
eststo: reg ratmanaged_sum i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id)
eststo: reg rattotal_onsite_waste_man  i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id)

esttab using "results\appendixtables.tex", drop(_cons) ///
	title("AT5 Panel A: No FE Global RDD (Waste Management)") stats(N fixed, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
	
eststo clear
eststo: reghdfe ratrelease_sum i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id) abs(ncasrn year)
eststo: reghdfe rattotal_onsite_releases i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id)  abs(ncasrn year)
eststo: reghdfe ratmanaged_sum i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id) abs(ncasrn year)
eststo: reghdfe rattotal_onsite_waste_man i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster id) abs(ncasrn year)

esttab using "results\appendixtables.tex", drop(_cons) ///
	title("AT5 Panel B: FE Global RDD (Waste Management)") stats(N fixed, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
	
	
*** Appendix Table 6: Global Regression Discontinuity (Third-Order Polynomial) - Air Pollution
*** only conventional estimates
preserve
duplicates drop uniqueid year, force
drop X T_X weights
gen X = nvshare-50
gen T_X = X * T
gen weights = .
replace weights = (1 - abs(X / 50 )) if X <= 0 & X >= -50 
replace weights = (1 - abs(X / 50 )) if X > 0 & X <= 50

eststo clear
eststo: reg aqione i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster uniqueid)
eststo: reg rataqione i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster uniqueid)

esttab using "results\appendixtables.tex", drop(_cons) ///
	title("AT6 Panel A: No FE Global RDD (Air Pollution)") stats(N fixed, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

eststo clear
eststo: reghdfe aqione i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster uniqueid) abs(year)
eststo: reghdfe rataqione i.T##(c.X##c.X##c.X) [aw = weights] if voters>=50 & city_samp==1, vce(cluster uniqueid) abs(year)

esttab using "results\appendixtables.tex", drop(_cons) ///
	title("AT6 Panel B: FE Global RDD (Air Pollution)") stats(N fixed, fmt(0 3)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
restore	

********************************************************************************
*** Appendix Table 7: Estimation Results for Contextual Robustness Tests

*** Change ratio threshold to 2
eststo clear
preserve
foreach v of varlist $ratios {
	replace `v'=. if `v'>2
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT7 Panel A: Ratio threshold at 2") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
restore

********************************************************************************

*** Ln of waste ratios as in Dutt 2014 (= exclude zeros)
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust l`v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	sum l`v' if voters>=50 & city_samp==1 & nvshare >=50-e(h_l) 
	estadd scalar mean=r(mean)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT7 Panel B:  Ln(ratios)") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Cumulative ratios in year t+3 
eststo clear
foreach v of varlist ratrelease_sum_cum rattotal_onsite_releases_cum ratmanaged_sum_cum rattotal_onsite_waste_man_cum {
	// replace `v'=. if `v'>9
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & year==electionyear+3, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT7 Panel C: Cumulative ratios") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

* Chemical year FE
tabulate casrn if voters>=50 & city_samp==1, generate(casdum)
tabulate year, generate(yeardum)
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id) covs(yeardum1-yeardum30 casdum1-casdum283)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT7 Panel D: Chemical-Year Fixed Effects") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
drop casdum* yeardum*

*** Number of voters --> 25 Not even close to significant
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=25 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT7 Panel E: Number of voters > 25") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Number of voters --> 75 Not even close to significant
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=75 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT7 Panel F: Number of voters > 75") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Robustness --> chemicals reported all the time
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & firstyr<=1990 & lastyr==., c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT7 Panel G: Regulated chemicals from 1990 till 2020") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append


*** Appendix Table 8: Estimation Results for Econometric Robustness Tests
*** Econometric Robustness --> Polynomial 2
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(2) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT8 Panel A: Second-order polynomial") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Econometric Robustness --> Epanechnikov distance
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(epa) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT8 Panel B: Epanechnikov kernel") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Econometric Robustness --> Donut
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & vshare!=50 & nvshare!=50, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT8 Panel C: Donut RDD") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append


*** Appendix Table 9: Estimation Results for Contextual and Econometric Robustness Tests AIR POLLUTION
preserve
duplicates drop uniqueid year, force
global ratios aqione rataqione
** Contextual Robustness
* year FE
tabulate year, generate(yeardum)
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid) covs(yeardum1-yeardum30)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT9 Panel A: Year Fixed Effects") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
drop  yeardum*

*** Number of voters --> 25 Not even close to significant
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=25 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT9 Panel B: Number of voters > 25") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Number of voters --> 75 Not even close to significant
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=75 & city_samp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT9 Panel C: Number of voters > 75") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Econometric Robustness --> Polynomial 2
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(triangular) p(2) bwselect(mserd) all vce(cluster uniqueid)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT9 Panel D: Second-order polynomial") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Econometric Robustness --> Epanechnikov distance
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) ///
	kernel(epa) p(1) bwselect(mserd) all vce(cluster uniqueid)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT9 Panel E: Epanechnikov kernel") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

*** Econometric Robustness --> Donut
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & vshare!=50 & nvshare!=50, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
title("AT9 Panel F: Donut RDD") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
restore

****************************************************
** FIGURE 3: Placebo Test
** Density of Discontinuity Estimates at Placebo Cutoffs
* waste management
use tri_nlrb_pollution_sample.dta, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

matrix X = J(2000, 1, 0)
forval i = 20(0.05)79 {
	qui rdrobust rattotal_onsite_waste_man nvshare  if voters>=50 & city_samp==1, c(`i') kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	scalar n=20*`i'
	matrix X[n,1] == e(tau_bc)
}
svmat X
replace X=. if X==0

eststo: rdrobust rattotal_onsite_waste_man nvshare if voters>=50 & city_samp==1, c(50)  kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
local coef `e(tau_bc)'

#delimit ;
hist X, xline(`coef', lcolor(black) lwidth(medthin) lpattern(dash)) 
	fcolor(edkblue*0.6) lcolor(edkblue)
	xtitle("Discontinuity estimate (bias-corrected)") ytitle("Density");
#delimit cr
graph export "results\placeboCutoff_oncure.pdf", as(pdf) replace
drop X


matrix X = J(2000, 1, 0)
forval i = 20(0.05)79 {
	qui rdrobust rattotal_onsite_releases nvshare  if voters>=50 & city_samp==1, c(`i') kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	scalar n=20*`i'
	matrix X[n,1] == e(tau_bc)
}
svmat X
replace X=. if X==0

eststo: rdrobust rattotal_onsite_releases nvshare if voters>=50 & city_samp==1, c(50)  kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
local coef `e(tau_bc)'
#delimit ;
hist X, xline(`coef', lcolor(black) lwidth(medthin) lpattern(dash)) 
	fcolor(edkblue*0.6) lcolor(edkblue)
	xtitle("Discontinuity estimate (bias-corrected)") ytitle("Density");
#delimit cr
graph export "results\placeboCutoff_onrelease.pdf", as(pdf) replace
drop X

* pollution
preserve
duplicates drop uniqueid year, force

matrix X = J(2000, 1, 0)
forval i = 20(0.05)79 {
	qui rdrobust aqione nvshare  if voters>=50 & city_samp==1, c(`i') kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
	scalar n=20*`i'
	matrix X[n,1] == e(tau_bc)
}
svmat X
replace X=. if X==0

eststo: rdrobust aqione nvshare if voters>=50 & city_samp==1, c(50)  kernel(triangular) p(1) bwselect(mserd) all vce(cluster uniqueid)
local coef `e(tau_bc)'
#delimit ;
hist X, xline(`coef', lcolor(black) lwidth(medthin) lpattern(dash)) 
	fcolor(edkblue*0.6) lcolor(edkblue)
	xtitle("Discontinuity estimate (bias-corrected)") ytitle("Density");
#delimit cr
graph export "results\placeboCutoff_aqione.pdf", as(pdf) replace
drop X
restore


********************************************************************************
************************************ Section 5 *********************************
use tri_nlrb_pollution_sample.dta, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

global ratios ratrelease_sum rattotal_onsite_releases ratmanaged_sum rattotal_onsite_waste_man
********************************************************************************
*** Table 6: Union Bargaining Power
* Right-to-Work law states (1=yes, 0=no)

eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & rtw==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	gen bbelow`v' = e(tau_bc)
	gen sebelow`v' = e(se_tau_rb) 
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
	title("T6 Panel A: RTW state") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & rtw==0, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	gen babove`v' = e(tau_bc)
	gen seabove`v' = e(se_tau_rb)
	gen b`v' = bbelow`v' - babove`v'
	gen  s1`v' = sebelow`v'^2
	gen  s2`v' = seabove`v'^2
	gen sc`v' = s1`v' + s2`v'
	gen  vdiff_`v' = abs(b`v' / sqrt(sc`v'))
	drop babove`v' seabove`v' bbelow`v' sebelow`v' b`v' s1`v' s2`v' sc`v'
	
	label variable vdiff_`v' "low and high union membership"
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
	title("T6 Panel B: No RTW state") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
	
	
	foreach v of varlist vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man   {
	di "Z-Score of difference testing for `v' is "  `v'
	if `v' >= 2.58 {
		di "Unionization effect is significantly (1%) different for `: variable label `v''"
		}
	else if  `v' >= 1.96 {
		di "Unionization effect is significantly (5%) different for `: variable label `v''"
		}
	else if `v' >= 1.65 {
		di "Unionization effect is significantly (10%) different for `: variable label `v''"
		}
	else {
		di "Unionization effect is not significantly different for `: variable label `v''"
		}
}	
drop vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man

********************************************************************************	
*** Appendix Table 10: Union Bargaining Power - State Union Density
*** Union coverage (BLS 2022), below above median
merge m:1 facility_state using unionmembership2022, gen(coveragemerge)
sum representedbyunionpercentof, d //median is 13.6
sum membersofunionpercentof, d //median is 12.7
***
* Union membership 
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 &membersofunionpercentof<12, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	gen bbelow`v' = e(tau_bc)
	gen sebelow`v' = e(se_tau_rb) 
}

esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT10 Panel A: Low union membership") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1  &membersofunionpercentof>=12, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	gen babove`v' = e(tau_bc)
	gen seabove`v' = e(se_tau_rb)
	gen b`v' = bbelow`v' - babove`v'
	gen  s1`v' = sebelow`v'^2
	gen  s2`v' = seabove`v'^2
	gen sc`v' = s1`v' + s2`v'
	gen  vdiff_`v' = abs(b`v' / sqrt(sc`v'))
	drop babove`v' seabove`v' bbelow`v' sebelow`v' b`v' s1`v' s2`v' sc`v'
	
	label variable vdiff_`v' "low and high union membership"
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT10 Panel B: High union membership") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append


foreach v of varlist vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man   {
	di "Z-Score of difference testing for `v' is "  `v'
	if `v' >= 2.58 {
		di "Unionization effect is significantly (1%) different for `: variable label `v''"
		}
	else if  `v' >= 1.96 {
		di "Unionization effect is significantly (5%) different for `: variable label `v''"
		}
	else if `v' >= 1.65 {
		di "Unionization effect is significantly (10%) different for `: variable label `v''"
		}
	else {
		di "Unionization effect is not significantly different for `: variable label `v''"
		}
}	
drop vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man

*** Appendix Table 11: Union Model Heterogeneity
****************************
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 &unionmodel=="organizing", c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	gen bbelow`v' = e(tau_bc)
	gen sebelow`v' = e(se_tau_rb)
}

esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT11 Panel A: Organizing model") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1  &unionmodel=="service", c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
	gen babove`v' = e(tau_bc)
	gen seabove`v' = e(se_tau_rb)
	gen b`v' = bbelow`v' - babove`v'
	gen  s1`v' = sebelow`v'^2
	gen  s2`v' = seabove`v'^2
	gen sc`v' = s1`v' + s2`v'
	gen  vdiff_`v' = abs(b`v' / sqrt(sc`v'))
	drop babove`v' seabove`v' bbelow`v' sebelow`v' b`v' s1`v' s2`v' sc`v'
	
	label variable vdiff_`v' "service and organizing union models"
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT11 Panel B: Service Model") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
	
	
foreach v of varlist vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man   {
	di "Z-Score of difference testing for `v' is "  `v'
	if `v' >= 2.58 {
		di "Unionization effect is significantly (1%) different for `: variable label `v''"
		}
	else if  `v' >= 1.96 {
		di "Unionization effect is significantly (5%) different for `: variable label `v''"
		}
	else if `v' >= 1.65 {
		di "Unionization effect is significantly (10%) different for `: variable label `v''"
		}
	else {
		di "Unionization effect is not significantly different for `: variable label `v''"
		}
}	
drop vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man


********************************************************************************
*** Table 7: Chemical Toxicity
* CERCLA RQs
gen cerclastrict=0
replace cerclastrict=1 if cercla_rq_lbs<=100

* CERCLA Strict == 1
eststo clear
foreach v of varlist $ratios {
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & cerclastrict==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) title("T7 Panel A: Cercla RQ less or equal to 100") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

* CERCLA Strict == 0
eststo clear
foreach v of varlist $ratios {
eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & cerclastrict==0, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) title("T7 Panel B: Cercla RQ above 100 (or missing)") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

********************************************************************************
*** Appendix Table 12: Chemical Toxicity – HTP
* HTP top half air == 1
eststo clear
foreach v of varlist $ratios {
eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & air_htp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT12 Panel A: High Air HTP") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
* HTP top half air == 0
eststo clear
foreach v of varlist $ratios {
eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & air_htp==0, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT12 Panel B: Low Air HTP") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
* HTP top half water == 1
eststo clear
foreach v of varlist $ratios {
eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & water_htp==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT12 Panel C: High Water HTP") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append
* HTP top half water == 0
eststo clear
foreach v of varlist $ratios {
eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & water_htp==0, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\appendixtables.tex", drop(Conventional Bias-corrected) ///
	title("AT12 Panel D: Low Water HTP") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

********************************************************************************
*** Table 8: Industry Affiliation
local posindus industry_highlev=="Foodstuffs" | industry_highlev=="Other durables" | ///
	industry_highlev=="Other" | industry_highlev=="Textile & apparel" | industry_highlev=="Wood & paper" | industry_highlev=="Wholesale"

gen posindus=0
replace posindus=1 if `posindus'

eststo clear
foreach v of varlist $ratios {
eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & posindus==0, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
	title("T8 Panel A: Heavy industry") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

eststo clear
foreach v of varlist $ratios {
eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1 & posindus==1, c(50) ///
	kernel(triangular) p(1) bwselect(mserd) all vce(cluster id)
}
esttab using "results\tables.tex", drop(Conventional Bias-corrected) ///
	title("T8 Panel B: Non-heavy industry") stats(N, fmt(0)) b(%9.3f) se nogap varw(10) ///
	label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

********************************************************************************
************************************ Section 6 *********************************
use tri_nlrb_pollution_sample, clear

keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election
gen X = nvshare-50
gen T_X = X * T
gen weights = .
gen optimalbw = .


********************************************************************************
*** Table 9: Production Output 
* Coefficients for the Change in the Production Output stored in list: e(beta_covs)
eststo clear
quietly rdrobust ratrelease_sum nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster id) 
replace weights = (1 - abs(X / e(h_l) )) if X <= 0 & X >= -e(h_l) 
replace weights = (1 - abs(X / e(h_l) )) if X > 0 & X <= e(h_l)
scalar h = e(h_l)
local h `e(h_l)' 
scalar b = e(b_l)
local b `e(b_l)'
reg prod_ratio_mean T##c.X [aw = weights] if X >= -h & X <=h & voters>=50 & city_samp==1, vce(cluster id)
eststo: rdrobust prod_ratio_mean nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster id) h(`h' `h') b(`b' `b')

foreach v of varlist $ratios {
	quietly rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) 	all vce(cluster id) 
	replace weights = (1 - abs(X / e(h_l) )) if X <= 0 & X >= -e(h_l) 
	replace weights = (1 - abs(X / e(h_l) )) if X > 0 & X <= e(h_l)
	scalar h = e(h_l)
	local h `e(h_l)' 
	scalar b = e(b_l)
	local b `e(b_l)'
	eststo: reg `v' prod_ratio_mean T##c.X [aw = weights] if X >= -h & X <=h & voters>=50 & city_samp==1, vce(cluster id)
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) 	all vce(cluster id) covs(prod_ratio_mean) h(`h' `h') b(`b' `b')
	matrix list e(beta_covs)
}

esttab using "results\tables.tex", drop(X) ///
title("T9: Production Ratio") b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

********************************************************************************
*** Table 10: Financial Constraints // not possible because NETS data is proprietary

********************************************************************************
*** Table 11: Prevention Activities
* Coefficients for the Change in the Production Output stored in list: e(beta_covs)
eststo clear
quietly rdrobust ratrelease_sum nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster id) 
replace weights = (1 - abs(X / e(h_l) )) if X <= 0 & X >= -e(h_l) 
replace weights = (1 - abs(X / e(h_l) )) if X > 0 & X <= e(h_l)
scalar h = e(h_l)
local h `e(h_l)' 
scalar b = e(b_l)
local b `e(b_l)'
eststo: rdrobust catastrophic_releases_dum nvshare if voters>=50 & city_samp==1 , c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster id) h(`h' `h') b(`b' `b')
eststo: rdrobust pp_inno nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) all vce(cluster id) h(`h' `h') b(`b' `b')

foreach v of varlist $ratios {
	quietly rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) 	all vce(cluster id) 
	replace weights = (1 - abs(X / e(h_l) )) if X <= 0 & X >= -e(h_l) 
	replace weights = (1 - abs(X / e(h_l) )) if X > 0 & X <= e(h_l)
	scalar h = e(h_l)
	local h `e(h_l)' 
	scalar b = e(b_l)
	local b `e(b_l)'
	eststo: reg `v' catastrophic_releases_dum pp_inno T##c.X [aw = weights] if X >= -h & X <=h & voters>=50 & city_samp==1, vce(cluster id)
	eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) 	all vce(cluster id) covs(catastrophic_releases_dum pp_inno) h(`h' `h') b(`b' `b')
	matrix list e(beta_covs)
		gen bbelow`v' = e(tau_cl)
	gen sebelow`v' = e(se_tau_cl)
		eststo: rdrobust `v' nvshare if voters>=50 & city_samp==1, c(50) kernel(triangular) p(1) bwselect(mserd) 	all vce(cluster id) h(`h' `h') b(`b' `b')
		gen babove`v' = e(tau_cl)
	gen seabove`v' = e(se_tau_cl)
	gen b`v' = bbelow`v' - babove`v'
	gen  s1`v' = sebelow`v'^2
	gen  s2`v' = seabove`v'^2
	gen sc`v' = s1`v' + s2`v'
	gen  vdiff_`v' = abs(b`v' / sqrt(sc`v'))
	drop babove`v' seabove`v' bbelow`v' sebelow`v' b`v' s1`v' s2`v' sc`v'
	
	label variable vdiff_`v' "with and without controls"
}

esttab using "results\tables.tex", drop(X) ///
title("T11: Prevention Activities") stats(controls, fmt(0)) b(%9.3f) se nogap varw(10) ///
label star(* 0.10 ** 0.05 *** 0.01) booktabs alignment(D{.}{.}{-1}) append

foreach v of varlist vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man   {
	di "Z-Score of difference testing for `v' is "  `v'
	if `v' >= 2.58 {
		di "Unionization effect is significantly (1%) different for `: variable label `v''"
		}
	else if  `v' >= 1.96 {
		di "Unionization effect is significantly (5%) different for `: variable label `v''"
		}
	else if `v' >= 1.65 {
		di "Unionization effect is significantly (10%) different for `: variable label `v''"
		}
	else {
		di "Unionization effect is not significantly different for `: variable label `v''"
		}
}	
drop vdiff_ratrelease_sum vdiff_rattotal_onsite_releases vdiff_ratmanaged_sum vdiff_rattotal_onsite_waste_man


********************************************************************************
************************************* Appendix *********************************

********************************************************************************	
*** Appendix Table 2: Descriptive Statistics
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

preserve
duplicates drop case_number, force // One observation per election
local f (fmt(2))
estpost tabstat vshare voters if voters>=50 & city_samp==1, statistics(mean sd min p25 p50 p75 max) col(stat)
esttab . using "results\appendixtables.tex", ///
	cells("mean`f' sd`f' min`f'  p25`f' p50`f' p75`f' max`f'") ///
	nonumbers modelw(10) title("AT2: Descriptive Statistics") label noobs booktabs alignment(D{.}{.}{-1}) append
restore

preserve
duplicates drop uniqueid year, force // Pollution at the Facility Year level
local f (fmt(2))
estpost tabstat aqione rataqione if voters>=50 & city_samp==1, statistics(mean sd min p25 p50 p75 max) col(stat)
esttab . using "results\appendixtables.tex", ///
	cells("mean`f' sd`f' min`f'  p25`f' p50`f' p75`f' max`f'") ///
	nonumbers modelw(10) title("AT2: Descriptive Statistics") label noobs booktabs alignment(D{.}{.}{-1}) append
restore

global allratios ratwaste_sum ratrelease_sum rattotal_onsite_releases rattotal_offsite_releases ratmanaged_sum rattotal_onsite_waste_man ratoffsite_waste_man 

local f (fmt(2))
estpost tabstat $allratios prod_ratio_mean pp_inno catastrophic_releases_dum if voters>=50 & city_samp==1, statistics(mean sd min p25 p50 p75 max) col(stat)
esttab . using "results\appendixtables.tex", ///
	cells("mean`f' sd`f' min`f'  p25`f' p50`f' p75`f' max`f'") ///
	nonumbers modelw(10) title(Descriptive Statistics) label noobs booktabs alignment(D{.}{.}{-1}) append




********************************************************************************
***** Map Graphs *****
* Appendix Figure 1
*** Load & Prepare Facility-Chemical-Level Data
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

duplicates drop case_number, force 
sort facility_state

by facility_state: gen election_count=_N

keep facility_state election_count
duplicates drop
drop if missing(facility_state)
rename facility_state statecode

* Merge TRI with state map shapefile
*first create usdb and uscoord from us.shp: shp2dta using us, database(usdb) coordinates(uscoord) genid(id) //(already done)

merge 1:1 statecode using usdb.dta

* Drop island/remote states
drop if inlist(statecode,"as","gu","mp","vi")
drop if NAME=="Alaska" // _ elections
drop if NAME=="Hawaii" // _ election
drop if NAME=="Puerto Rico" // _ elections

* Create Graph
# delimit ;
spmap election_count using uscoord, id(stateid) fcolor(Blues) 
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapElectionsAll.pdf", replace



*** Map of Elections by State - Sample
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

keep if voters >=50 & city_samp==1

duplicates drop case_number, force
sort facility_state

by facility_state: gen election_count=_N

keep facility_state election_count
duplicates drop
drop if missing(facility_state)
rename facility_state statecode

* Merge TRI sample with state map shapefile
merge 1:1 statecode using usdb.dta

* Drop island/remote states
drop if inlist(statecode,"as","gu","mp","vi")
drop if NAME=="Alaska" // 0 elections
drop if NAME=="Hawaii" // 1 election
drop if NAME=="Puerto Rico" // 9 elections


* Create Graph
# delimit ;
spmap election_count using uscoord, id(stateid) fcolor(Blues)
	clmethod(custom) clb(1 4 10 25 64)
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapElectionsSample.pdf", replace


*** Observations by state in sample
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

keep if voters >=50 & city_samp==1

bysort facility_state: gen obs_count=_N
keep facility_state obs_count
duplicates drop
drop if missing(facility_state)
rename facility_state statecode

* Merge TRI sample with state map shapefile
merge 1:1 statecode using usdb

* Drop island/remote states
drop if inlist(statecode,"as","gu","mp","vi")
drop if NAME=="Alaska" // 0 elections
drop if NAME=="Hawaii" // 1 election
drop if NAME=="Puerto Rico" // 9 elections

* Create Graph
# delimit ;
spmap obs_count using uscoord, id(stateid) fcolor(Blues)
clmethod(custom) clb(2 30 100 200 1000)
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapObservationsSample.pdf", replace


*** Average chemicals reported by state in sample
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

keep if voters >=50 & city_samp==1

bysort trifd year: gen chem_count=_N
duplicates drop trifd year, force
bysort facility_state: egen chem_count_avg=mean(chem_count)
bysort facility_state: egen chem_count_p50=median(chem_count)

keep facility_state chem_count_avg chem_count_p50
duplicates drop 
drop if missing(facility_state)
rename facility_state statecode

* Merge TRI sample with state map shapefile
merge 1:1 statecode using usdb

* Drop island/remote states
drop if inlist(statecode,"as","gu","mp","vi")
drop if NAME=="Alaska" // 0 elections
drop if NAME=="Hawaii" // 1 election
drop if NAME=="Puerto Rico" // 9 elections

format chem_count_avg chem_count_p50 %9.0f

* Create Graph
# delimit ;
spmap chem_count_avg using uscoord, id(stateid) fcolor(Blues)
	clmethod(custom) clb(1 2 3 5 15)
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapAvgChemsSample.pdf", replace

# delimit ;
spmap chem_count_p50 using uscoord, id(stateid) fcolor(Blues)
	clmethod(q)
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapP50ChemsSample.pdf", replace


********************************************************************************
*** Map of Waste and Waste Decomposition and RTW
use tri_nlrb_pollution_sample, clear
keep if year>electionyear // Post Election Effects
keep if year<=electionyear+3 //Up to three years post election

keep if voters >=50 & city_samp==1

* As in sample: change ratios to missing if larger than 3
global ratios ratrelease_sum rattotal_onsite_releases rattotal_offsite_releases ratmanaged_sum rattotal_onsite_waste_man ratoffsite_waste_man
foreach v of varlist $ratios ratwaste_sum {
	replace `v'=. if `v'>3
}

local keepvars waste_sum release_sum managed_sum prod_ratio_mean ratwaste_sum ratrelease_sum ratmanaged_sum rattotal_onsite_releases rattotal_onsite_waste_man
local keepvarsp50
foreach v of local keepvars {
	gen `v'_p50=`v'
	local keepvarsp50 `keepvarsp50' `v'_p50
}
collapse (mean) `keepvars' (median) `keepvarsp50' rtw2, by(facility_state) 

drop if missing(facility_state)
rename facility_state statecode

* Merge TRI sample with state map shapefile
merge 1:1 statecode using usdb

* Drop island/remote states
drop if inlist(statecode,"as","gu","mp","vi")
drop if NAME=="Alaska" // 0 elections
drop if NAME=="Hawaii" // 1 election
drop if NAME=="Puerto Rico" // 9 elections


* Adjust to million pounds (or '000 pounds) 
foreach v of varlist waste_sum-managed_sum waste_sum_p50-managed_sum_p50 {
	replace `v'=`v'/1000000
}

* Change display format to 2 decimals
format waste_sum-rattotal_onsite_waste_man_p50 %9.2f

* Create Graph with Prod Ratio
# delimit ;
spmap prod_ratio_mean using uscoord, id(stateid) fcolor(Blues) 
	clmethod(q)
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapProdRatioSample.pdf", replace

* Create Graph with Waste Treated ratio
# delimit ;
spmap ratmanaged_sum using uscoord, id(stateid) fcolor(Blues)
	clmethod(custom) clb(0.47 0.9 1 1.1 1.4)
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapTreatChangeSample.pdf", replace

* Create Graph with Waste Release ratio
# delimit ;
spmap ratrelease_sum using uscoord, id(stateid) fcolor(Blues)
	clmethod(custom) clb(0.7 0.9 1 1.1 1.2)
	legstyle(1) legend(size(huge) pos(5)) name(maplarge,replace);
# delimit cr
graph export "results\MapReleaseChangeSample.pdf", replace


